package com.example.dao;

import java.sql.*;

/**
 * 用户数据访问对象
 * 连接本地MySQL数据库的student库，验证users表的登录信息
 */
public class UserDAO {

    // 数据库连接配置（根据你的实际环境修改）
    private static final String DB_URL = "jdbc:mysql://localhost:3306/学生表?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8";
    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "admin";
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";

    /**
     * 验证用户登录（连接真实数据库）
     * @param username 对应数据库中LoginId字段
     * @param password 对应数据库中LoginPwd字段
     * @return 验证成功返回true，否则返回false
     */
    public boolean validateUser(String username, String password) {
        if (username == null || password == null) {
            return false;
        }

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            // 加载数据库驱动
            Class.forName(DRIVER_NAME);

            // 建立数据库连接
            conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);

            // 查询用户信息（关联LoginId和LoginPwd字段）
            String sql = "SELECT LoginPwd FROM users WHERE LoginId = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);

            rs = pstmt.executeQuery();

            // 验证密码
            if (rs.next()) {
                String storedPassword = rs.getString("LoginPwd");
                return storedPassword != null && storedPassword.equals(password);
            }

        } catch (ClassNotFoundException e) {
            System.err.println("找不到数据库驱动类: " + e.getMessage());
        } catch (SQLException e) {
            System.err.println("数据库操作异常: " + e.getMessage());
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                System.err.println("关闭数据库连接异常: " + e.getMessage());
            }
        }

        return false;
    }

    /**
     * 检查用户是否存在
     * @param username 对应数据库中LoginId字段
     * @return 用户存在返回true，否则返回false
     */
    public boolean userExists(String username) {
        if (username == null) {
            return false;
        }

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            // 加载数据库驱动
            Class.forName(DRIVER_NAME);

            // 建立数据库连接
            conn = DriverManager.getConnection(DB_URL, DB_USERNAME, DB_PASSWORD);

            // 查询用户是否存在
            String sql = "SELECT COUNT(*) FROM users WHERE LoginId = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, username);

            rs = pstmt.executeQuery();

            if (rs.next()) {
                return rs.getInt(1) > 0;
            }

        } catch (ClassNotFoundException e) {
            System.err.println("找不到数据库驱动类: " + e.getMessage());
        } catch (SQLException e) {
            System.err.println("数据库操作异常: " + e.getMessage());
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                System.err.println("关闭数据库连接异常: " + e.getMessage());
            }
        }

        return false;
    }
}